package com.tsingteng.cosfun.ui.costar.drafts;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.qiniu.pili.droid.shortvideo.PLDraft;
import com.qiniu.pili.droid.shortvideo.PLDraftBox;
import com.tsingteng.cosfun.R;
import com.tsingteng.cosfun.base.BaseActivity;
import com.tsingteng.cosfun.bean.DBBean;
import com.tsingteng.cosfun.bean.DBPlayBean;
import com.tsingteng.cosfun.utils.DBUtils;
import com.tsingteng.cosfun.utils.NotchUtil;
import com.tsingteng.cosfun.widget.DraftsHeaderView;
import com.tsingteng.cosfun.widget.TitleView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 描述 :草稿箱
 * 创建人: 肖利.
 * 时间:2018/8/10
 * qq(872179966)
 */

public class DraftsAcitvity extends BaseActivity implements DraftsHeaderView.OnDraftsHeaderClickListener,View.OnClickListener{

    @BindView(R.id.dh_drafts)
    DraftsHeaderView dhDrafts;
    @BindView(R.id.drafts_title_view)
    RelativeLayout mTvTitle;
    @BindView(R.id.drafts_view)
    View mDraftsView;
    @BindView(R.id.drafts_iv_left)
    ImageView mDraftsIvLeft;

    private List<Fragment> fragmentList  = new ArrayList<>();
    private Fragment mVideoDraftsFragment ;
    private Fragment mPlayDraftsFragment ;
    private int currentPosition = -1;


    @Override
    protected void initView() {
        isFound = true;
        mDraftsIvLeft.setOnClickListener(this);
        if (NotchUtil.isAllScreenDevice(this)){
            mDraftsView.setVisibility(View.GONE);
        }else{
            mDraftsView.setVisibility(View.VISIBLE);
        }
        List<DBPlayBean> play = DBUtils.selectPlayDB("play");
        List<PLDraft> mDrafts = PLDraftBox.getInstance(this).getAllDrafts();
        dhDrafts.setIvWorkText(mDrafts.size()+"");
        dhDrafts.setPlayText(play.size()+"");
        dhDrafts.setOnDraftsHeaderClickListener(this);
        if (mVideoDraftsFragment == null) mVideoDraftsFragment = new VideoDraftsFragment();
        if (mPlayDraftsFragment == null) mPlayDraftsFragment = new PlayDraftsFragment();
        fragmentList.add(mVideoDraftsFragment);
        fragmentList.add(mPlayDraftsFragment);

        chageFragment(0,currentPosition,fragmentList);
        currentPosition =0;
        dhDrafts.changUI(true);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_drafts;
    }

    @Override
    protected void getIntentData(Intent intent) {

    }

    /**
     * 切换fragment
     * @param position
     * @param currentPosition
     * @param fragmentList
     */
    protected void chageFragment(int position, int currentPosition, List<Fragment> fragmentList) {
        if (currentPosition == position) return;
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        if (currentPosition != -1)
            fragmentTransaction.hide(fragmentList.get(currentPosition));
        if (!fragmentList.get(position).isAdded()) {

            fragmentTransaction.add(R.id.contentContainer, fragmentList.get(position));
        }
        fragmentTransaction.show(fragmentList.get(position)).commitAllowingStateLoss();
    }

    @Override
    public void onWorkClick() {
        chageFragment(0,currentPosition,fragmentList);
        currentPosition=0;
    }

    @Override
    public void onPlayClick() {
        chageFragment(1,currentPosition,fragmentList);
        currentPosition=1;
    }

    public void setVideoNumber(String videoNumber) {
        dhDrafts.setIvWorkText(videoNumber);
    }

    public void setPlayNumber(String playNumber) {
        dhDrafts.setPlayText(playNumber);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.drafts_iv_left:
                finish();
                break;
        }
    }
}
